package Constants;

public class ProtocolConsts {
	
	public static final int _NUM_SEQ = 0;
	public static final int _NUM_REC = 1;
	public static final int _FLAG_ACK = 2;
	public static final int _FLAG_RST = 3;
	public static final int _FLAG_SYN = 4;
	public static final int _FLAG_FIN = 5;
	public static final int _JAN_RCPT = 6;
	
	public static final int _BIT_ACK = 1;
	public static final int _BIT_RST = 2;
	public static final int _BIT_SYN = 4;
	public static final int _BIT_FIN = 8;

	public static final int _HEADER_SIZE = 11;
	public static final int _MSS = 1013;
	public static final int _MAX_BUFFER_SIZE = 64832;
	
	public static byte[] intToByteArray(int num, int howManyBytes) {
		byte convert[];
		if (howManyBytes == 4) {
			convert = new byte[4];
			convert[0] = (byte) num;
			convert[1] = (byte) (num>>8);
			convert[2] = (byte) (num>>16);
			convert[3] = (byte) (num>>24);
		} else if (howManyBytes == 2) {
			convert = new byte[2];
			convert[0] = (byte) num;
			convert[1] = (byte) (num>>8);
		} else {
			convert = new byte[1];
			convert[0] = (byte) num;
		}
		return convert;
	}

	public static int byteArrayToInt(byte[] num, int field, int howManyBytes, int flagId /*-1 == NONE, 0 == ACK, 1 == RST, 2 == SYN, 3 == FIN*/) {
		if (howManyBytes == 4)
			return ((int)((0xff&num[(field<<2)+3])<<24)|((0xff&num[(field<<2)+2])<<16)|((0xff&num[(field<<2)+1])<<8)|((0xff&num[field<<2])));
		else if (howManyBytes == 2)
			return ((int)((0xff&num[(field<<1)+4])<<8)|(0xff&num[(field<<1)+3]));
		else
			return (int)(0xff&num[8]&(1<<(7-flagId))) > 0 ? 1 : 0;
	}
}
